package com.demo.controller;

import com.demo.service.DatasourceService;
import com.demo.service.PageService;
import com.demo.vo.OrderTemplate;
import com.demo.vo.TemplatesParam;
import com.yiyi.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @author sdyang
 * @create 2019-01-25 9:23
 **/
@Controller
public class PageDesignController {

    @Autowired
    private PageService pageService;
    @Autowired
    private DatasourceService datasourceService;

    @GetMapping("page_design")
    public String pageDesign(Model model) {
        return "demo1";
    }

    @PostMapping("templates")
    @ResponseBody
    public String temples(TemplatesParam params) {
        return JsonUtil.toJSONString(getTestData());
//        return pageService.queryTemplate(params);
    }


    @PostMapping("templateDetail")
    @ResponseBody
    public OrderTemplate templeDetail(Long id) {
        OrderTemplate template = new OrderTemplate();
        template.setId(1L);
        template.setCode("test1");
        template.setName("测试1");
        template.setHead("PG1ldGEgY2hhcnNldD0iVVRGLTgiPgo8bWV0YSBuYW1lPSJ2aWV3cG9ydCIgY29udGVudD0id2lkdGg9ZGV2aWNlLXdpZHRoLGluaXRpYWwtc2NhbGU9MSx1c2VyLXNjYWxhYmxlPTAsdmlld3BvcnQtZml0PWNvdmVyIj4KPHRpdGxlPuWBpeW6t+aho+ahiOWIl+ihqDwvdGl0bGU+CjxsaW5rIHJlbD0ic3R5bGVzaGVldCIgaHJlZj0iJHtXT0xGX0JBU0VfRElSfXN0eWxlL2NvbW1vbi5jc3MiIC8+CjxsaW5rIHJlbD0ic3R5bGVzaGVldCIgaHJlZj0iJHtXT0xGX0JBU0VfRElSfXN0eWxlL3dldWkuY3NzIiAvPgo8bGluayByZWw9InN0eWxlc2hlZXQiIGhyZWY9IiR7V09MRl9CQVNFX0RJUn1zdHlsZS93YXAuY3NzIiAvPgo8c2NyaXB0IHNyYz0iJHtXT0xGX0J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PSJwYWdlX2RhdGEiPgogIDxkaXYgY2xhc3M9InRhYmJhci10aXRsZSI+CiAgICA8YSBocmVmPSJqYXZhc2NyaXB0Omhpc3RvcnkuZ28oLTEpOyIgY2xhc3M9Imljb24tYmFjayI+CiAgICAgIDxzcGFuIGNsYXNzPSJpY29uIj7ina48L3NwYW4+PC9hPgogICAgPHNwYW4gY2xhc3M9InRpdGxlLXRleHQiPuWBpeW6t+aho+ahiDwvc3Bhbj48L2Rpdj4KICA8ZGl2IGNsYXNzPSJ3ZXVpLWNlbGxzIj4KICAgIDxhIGNsYXNzPSJ3ZXVpLWNlbGwgd2V1aS1jZWxsX2FjY2VzcyIgaHJlZj0iamF2YXNjcmlwdDp2b2lkKDApOyIgQGNsaWNrLnByZXZlbnQ9ImdvdG9Eb2N0b3IoZmlsZV9saXN0LmlkKSIgdi1mb3I9ImZpbGVfbGlzdCBpbiBmaWxlX2xpc3QiPgogICAgICA8ZGl2IGNsYXNzPSJ3ZXVpLWNlbGxfX2JkIj4KICAgICAgICA8cD57e2ZpbGVfbGlzdC51c2VyX25hbWV9fTwvcD4KICAgICAgPC9kaXY+CiAgICAgIDxkaXYgY2xhc3M9IndldWktY2VsbF9fZnQiPjwvZGl2PgogICAgPC9hPgogIDwvZGl2PgogCgo8ZGl2IGNsYXNzPSJ3ZXVpLWJ0bi1hcmVhIGZvb3Rlci1idG4tZGl2IiA+CiAgICA8YSBjbGFzcz0id2V1aS1idG4gd2V1aS1idG5fcHJpbWFyeSBuby1ib3JkZXItcmFkaXVzIiBocmVmPSIvcGFnZS9mNTVlODJmZC1iZTIzLTQ5N2ItOGEwMS02ZTJlZWVhMWI5MDg/cGFyYW09WTJ4cFpXNTBQV1JoZVdGM1lXND0iIGlkPSJzaG93VG9vbHRpcHMiPivmt7vliqDmiJDlkZg8L2E+CjwvZGl2Pgo8L2Rpdj4=");
        return template;
    }

    @PostMapping("getDatasource")
    @ResponseBody
    public String getDatasource(String key) {
        return JsonUtil.toJSONString(getTestData());
//        return datasourceService.query(new TemplatesParam());
    }

    @PostMapping("saveTemplate")
    @ResponseBody
    public String saveTemplate(OrderTemplate template) {


        System.out.println(template.getHead());
        return "success";
    }


    private List<OrderTemplate> getTestData() {
        List<OrderTemplate> templates = new ArrayList<OrderTemplate>();

        OrderTemplate template = new OrderTemplate();
        template.setId(1L);
        template.setCode("test1");
        template.setName("测试1");
        templates.add(template);

        OrderTemplate template1 = new OrderTemplate();
        template1.setId(2L);
        template1.setCode("test2");
        template1.setName("测试2");
        templates.add(template1);

        return templates;
    }
}
